//
// Created by Administrator on 2024/5/6.
//
#include <jni.h>
#include "libyuv.h"
#include "LibYUVConvert.h"

extern "C"
JNIEXPORT void JNICALL
Java_com_jsx_nativelibs_ffmpeg_LibYUV_argb2Yuv420P(JNIEnv *env, jobject clazz, jlong context,
                                                   jbyteArray argb_in, jbyteArray yuv_out) {
    LibYUVConvert *libYuvConvert = reinterpret_cast<LibYUVConvert *>(context);
    uint8_t *src_argb = reinterpret_cast<uint8_t *>(env->GetByteArrayElements(argb_in, JNI_FALSE));
    uint8_t *argbToI420 = libYuvConvert->argbToI420(src_argb);
    env->SetByteArrayRegion(yuv_out, 0, libYuvConvert->getLen(),
                            reinterpret_cast<const jbyte *>(argbToI420));
    env->ReleaseByteArrayElements(argb_in, reinterpret_cast<jbyte *>(src_argb), JNI_FALSE);
}
extern "C"
JNIEXPORT jlong JNICALL
Java_com_jsx_nativelibs_ffmpeg_LibYUV_init(JNIEnv *env, jobject thiz, jint width, jint height) {
    LibYUVConvert *libYuvConvert = new LibYUVConvert(width, height);
    return reinterpret_cast<jlong>(libYuvConvert);
}
extern "C"
JNIEXPORT void JNICALL
Java_com_jsx_nativelibs_ffmpeg_LibYUV_destroy(JNIEnv *env, jobject thiz, jlong context) {
    LibYUVConvert *libYuvConvert = reinterpret_cast<LibYUVConvert *>(context);
    delete libYuvConvert;
}
